package com.jeeplus.config.shiro;
import com.jeeplus.common.utils.StringUtils;
import lombok.Data;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.List;
@Configuration
@WebFilter(filterName = "refererFilter", urlPatterns = "/*")
@Data
public class RefererFilter implements Filter {
    public static final Logger logger = LoggerFactory.getLogger(RefererFilter.class);

    /**
     * 过滤器配置对象
     */
    FilterConfig filterConfig = null;

    /**
     * 是否启用
     */
    private boolean enable = true;

    /**
     * 忽略的URL
     */
    private String excludes = "";

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        this.filterConfig = filterConfig;
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
            throws IOException, ServletException {
//        HttpServletRequest request = (HttpServletRequest) servletRequest;
//        String referer = request.getHeader("Referer");
//        String serverName = request.getServerName();
//        // 判断是否存在外链请求本站
//        if (referer == null || !referer.contains(serverName)) {
//            if(null != referer && referer.contains("tfzw.tfgyc.com")){
//                filterChain.doFilter(servletRequest, servletResponse);
//            }else{
//                logger.error("Referer过滤器 服务器：{} 当前域名：{}", serverName, referer);
//                HttpServletResponse resp = (HttpServletResponse) servletResponse;
//                resp.setStatus(HttpStatus.NOT_FOUND.value());
//                resp.setContentType("application/json; charset=utf-8");
//                String json = "{\"msg\":\"拒绝访问\",\"success\":false,\"code\":\""+HttpStatus.NOT_FOUND.value()+"\"}";
//                PrintWriter writer = resp.getWriter();
//                writer.print(json);
//                writer.flush();
//                writer.close();
//                return;
//            }
//        } else {
//            filterChain.doFilter(servletRequest, servletResponse);
//        }
        filterChain.doFilter(servletRequest, servletResponse);
    }


    @Override
    public void destroy() {
        this.filterConfig = null;
    }

    /**
     * 判断是否为忽略的URL
     * <p>
     * <p>
     * URL路径
     *
     * @return true-忽略，false-过滤
     */
    private boolean isExcludeUrl(String url) {
        if (StringUtils.isBlank(excludes)) {
            return false;
        }
        List<String> urls = Arrays.asList(excludes.trim().split(","));
        for (String uri : urls) {
            // 正则验证
            if(uri.contains(url)){
                return true;
            }
        }
        return false;
    }
}